﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Pacjent.referencja1;
using System.Windows.Navigation;


namespace Pacjent
{
    public partial class pznajdz : PhoneApplicationPage
    {
        static referencja1.Service1SoapClient c;
        string idchor = "0";
        string choroba = "";
        
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {   string separator = "^^";
            string peselpar = "";
            if (NavigationContext.QueryString.TryGetValue("data", out peselpar))
            {
                string[] podzielone = peselpar.Split(separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                c.pobierzpacjentaAsync(podzielone[0]);               
                choroba = podzielone[1];           
            }                                                        
        }

        public pznajdz()
        {
            InitializeComponent();
            c = new referencja1.Service1SoapClient();      
            c.pobierzpacjentaCompleted += new EventHandler<pobierzpacjentaCompletedEventArgs>(c_pobierzpacjentaCompleted);
            c.updpacjentCompleted += new EventHandler<updpacjentCompletedEventArgs>(c_updpacjentCompleted);
            c.pobierzchorobeCompleted += new EventHandler<pobierzchorobeCompletedEventArgs>(c_pobierzchorobeCompleted);
            c.updchorobaCompleted += new EventHandler<updchorobaCompletedEventArgs>(c_updchorobaCompleted);
        }

        void c_updchorobaCompleted(object sender, updchorobaCompletedEventArgs e)
        {
            MessageBox.Show("poprawnie zmodyfikowano chorobe");
        }

        void c_pobierzchorobeCompleted(object sender, pobierzchorobeCompletedEventArgs e)
        {
            if (e.Result.ToString() != "s")
            {
                string separator = "%$&";
                string sepdat1 = "/";
                string sepdat2 = " ";       
                string[] podzielone = e.Result.Split(separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                string[] podzieldat = podzielone[2].Split(sepdat1.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                string[] podzieldata = podzieldat[2].Split(sepdat2.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                textBox11.Text = podzieldata[0] +"-"+podzieldat[0]+"-"+podzieldat[1];
                textBox12.Text = podzielone[3];
                string[] podzieldat2 = podzielone[4].Split(sepdat1.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                string[] podzieldata2 = podzieldat2[2].Split(sepdat2.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                textBox13.Text = podzieldata2[0] + "-" + podzieldat2[0] + "-" + podzieldat2[1]; 
                textBox14.Text = podzielone[5];
                idchor = podzielone[0];
                           
            }
            else
                MessageBox.Show("nie znaleziono podanej choroby u pacjenta!");
        }

        void c_updpacjentCompleted(object sender, updpacjentCompletedEventArgs e)
        {
            MessageBox.Show("poprawnie zmodyfikowano pacjenta");
        }

        void c_pobierzpacjentaCompleted(object sender, pobierzpacjentaCompletedEventArgs e)
        {
            
            if (e.Result.ToString() != "s")
            {
                string separator = "%$&";
                string[] podzielone = e.Result.Split(separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                textBox2.Text = podzielone[0];
                textBox3.Text = podzielone[1];
                textBox4.Text = podzielone[2];
                textBox9.Text = podzielone[3];
                textBox6.Text = podzielone[4];
                textBox7.Text = podzielone[5];
                if (choroba == "")
                {
                    textBox8.Text = podzielone[6];
                    textBox5.Text = podzielone[7];
                }
                else
                {
                    textBox8.Text = podzielone[6] + ", " + choroba;
                    textBox5.Text = choroba;
                }
                    
                pivot1.Title = "Pacjent: " + podzielone[0] +" "+ podzielone[1] + " " + textBox1.Text;               

            } 
            else
                MessageBox.Show("nie znaleziono pacjenta o podanym numerze pesel!");            
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void PowrotDoMenu()
        {

            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void hyperlinkButton2_Click(object sender, RoutedEventArgs e)
        {
            PowrotDoMenu();
        }

        private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
        {
            PowrotDoMenu();
        }

        private void hyperlinkButton3_Click(object sender, RoutedEventArgs e)
        {
            PowrotDoMenu();
        }

        private void hyperlinkButton4_Click(object sender, RoutedEventArgs e)
        {
            PowrotDoMenu();
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {            
            NavigationService.Navigate(new Uri("/pdodajchor.xaml?pesel="+textBox1.Text+"", UriKind.Relative));                    
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            c.pobierzpacjentaAsync(textBox1.Text);            
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox9.Text = ""; 
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            textBox5.Text = "";
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            c.pobierzchorobeAsync(textBox1.Text, textBox10.Text);
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            if (textBox2.Text != "")
            {
                c.updpacjentAsync(textBox2.Text, textBox3.Text, textBox1.Text, textBox4.Text, textBox9.Text, textBox6.Text, textBox7.Text, textBox8.Text, textBox5.Text, textBox1.Text);
            }
            if (textBox10.Text != "") { 
            c.updchorobaAsync(idchor,textBox10.Text,textBox11.Text,textBox12.Text,textBox13.Text,textBox1.Text,textBox14.Text,textBox10.Text,textBox1.Text);
            }
         
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/wykres.xaml", UriKind.Relative));
        }        
    }
}